package com.playmoney.service;

import java.util.Date;

import org.apache.log4j.Logger;

import com.followman.framework.ResultMap;
import com.playmoney.common.ResultCode;
import com.playmoney.dao.UserDAO;
import com.playmoney.model.User;

public class UserService {
	private Logger log = Logger.getLogger(UserService.class);

	private UserDAO userDAO = null;

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	/**
	 * 插入用戶信息
	 * @param isAdmin
	 * @param phone
	 * @param inTime
	 * @param groupStr
	 * @param sex
	 * @param company
	 * @param remark
	 * @return
	 */
	public ResultMap<ResultCode, User> insert(int isAdmin,String phone,Date inTime,
			String groupStr,byte sex, String company, String remark) {
		ResultMap<ResultCode, User> result = new ResultMap<ResultCode, User>();
		
		if(inTime == null || "".equals(inTime)){
			inTime = new Date();
		}
		if(groupStr == null || "".equals(groupStr)){
			groupStr = System.currentTimeMillis()+"";
		}
		
		User user = new User();
		user.setCompany(company);
		user.setCreateTime(new Date());
		user.setGroupStr(groupStr);
		user.setInTime(inTime);
		user.setIsAdmin(isAdmin);
		user.setPhone(phone);
		user.setRemark(remark);
		user.setSex(sex);
		
		int userId = 0;
		try {
			userId = this.userDAO.insert(user);
			if(userId != 0){
				user.setUserId(userId);
				result.setCode(ResultCode.OK);
				result.setResult(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		}
		return result;
	}
}
